import axios from 'axios'
import router from '../router'

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('intrachaincode')) {
      config.headers.Authorization = localStorage.getItem('intrachaincode')
    }
    return config
  },
  error => {
    return Promise.reject(error)
  })
// response全局拦截
axios.interceptors.response.use(
  response => {
    if (response.data.code === 403 || response.data.code === 401) {
      router.push({
        path: '/login'
      })
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)
// let apis = ''
// if (window.location.href.includes('blockmallapi')) {
//   apis = 'https://h5.blockmallapi.com'
// }else{
//   apis = 'http://192.168.0.179:8000'
// }
// let apis = 'http://47.56.149.232:8000'
// let apis = 'http://192.168.0.66:8000'
let apis = 'http://192.168.0.179:8000'
// let apis = 'https://h5.blockmallapi.com'
// let apis = ''

// 登录
export const login = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/member/login/login',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取商品分类
export const goodsCategory = () => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoodsCategory/listGoodsCategory',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取商品规格 "categoryId": 0,
export const getGoodsInfo = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoodsAttr/queryGoodsAttr',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取运费模板列表
export const getStoreFreightTemList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreFreightTem/getList',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 新建运费模板
export const addStoreFreightTemList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreFreightTem/add',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新运费模板
export const updateStoreFreightTemList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreFreightTem/update',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除运费模板
export const deleteStoreFreightTemList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreFreightTem/delete',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取支付模板
export const getPayTemplate = () => {
  return axios({
    method: 'POST',
    url: apis + '/store/payTemplate/queryAllPayTemplate',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 添加商品
export const addGoods = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/addGoods',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取商品列表 storeId
export const getGoodsList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/getGoodsList',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取商品列表 storeId
export const goodShelves = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/shelves',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除商品
export const delGoods = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/delGoods',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询商品
export const findGoods = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/findGoods',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 修改商品信息
export const updateGoods = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/updateGoods',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 订单列表
export const orderListApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/findOrders',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
//  订单详情
export const orderDetailApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/orderDetail',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
//  退款订单详情
export const orderRdfundDetailApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/refundDetail',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 确认退款
export const sureRefundApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/refundConfirm',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 查询快递厂商
export const searchExpressCompanyApi = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/order/xlOrdersExpress/queryAllExpressCompany',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 取消订单
export const cancelOrderApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/cancelOrder',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 删除订单
export const deleteOrderApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/deleteOrder',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 查询物流
export const expressOrderApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/expressDetail',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 订单退款(商家同意) 1 同意 2不同意
export const backMonenyOrderApi = (data, type) => {
  return axios({
    method: 'POST',
    url: apis + (type === 1 ? 'order/xlOrder/refundAgree' : 'order/xlOrder/refundRefuse'),
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 订单发货

export const sendOrderApi = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/order/xlOrder/sendOrder',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
// 查询子评价
export const findSonComment = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoodsComment/findSonComment',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询评价
export const findSonCommentApp = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoodsComment/findSonCommentApp',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}


// 商家申诉
export const storeAppeal = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlCommentAppeal/appeal',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 商家回复/添加评论
export const addComment = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoodsComment/addComment',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询申诉列表
export const findAppeal = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlCommentAppeal/findAppeal',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取省市区
export const findAllArea = () => {
  return axios({
    method: 'POST',
    url: apis + '/member/xlRegion/findAllArea',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取店铺信息
export const getDetail = () => {
  return axios({
    method: 'GET',
    url: apis + '/store/xlStore/getStore'
  })
}

// 创建店铺
export const createStore = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStore/create',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新店铺信息
export const updateStore = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStore/modifyStoreInfo',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除店铺信息
export const deleteStore = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStore/delete',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询地址列表
export const listStoreRefund = (data) => {
  return axios({
    method: 'POST',
    data: JSON.stringify(data),
    url: apis + '/store/xlStoreRefundAdd/listStoreRefund',
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 新增售后地址
export const addStoreRefund = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreRefundAdd/addStoreRefund',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新售后地址
export const editStoreRefund = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreRefundAdd/editStoreRefund',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除售后地址
export const deleteStoreRefund = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreRefundAdd/deleteStoreRefund',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 修改库存价格
export const updateSpecs = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/goods/xlGoods/modifyStock',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 新增溯源服务模板 
export const addTraceTemplate = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/addTraceTemplate',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询商家溯源环节模板（分页）
export const getTraceLinkTemplatePageList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/getTraceLinkTemplatePageList',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取溯源模板
export const getBaseTraceProductInfo = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/template/v1/getBaseTraceProductInfo?goodsId=' + data
  })
}

// 删除溯源模板
export const deleteTraceTemplate = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/template/v1/deleteTraceTemplate?id=' + data
  })
}

// 编辑溯源模板
export const updateTraceTemplate = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/updateTraceTemplate',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询商家自定义溯源环节模板
export const getTraceOriginLinkTemplate = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/template/v1/getTraceOriginLinkTemplate?uid=' + data
  })
}

// 分页查询商品批次信息
export const getTraceProductBatchByPage = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/getTraceProductBatchByPage',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 新增溯源商品批次
export const addTraceProductBatch = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/addTraceProductBatch',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询选择溯源环节模板信息（含商品信息）
export const getTraceOriginLinkTemplateAndProduct = (data, data1) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/template/v1/getTraceOriginLinkTemplateAndProduct?batchId=' + data + "&&templateLinkId=" + data1,
  })
}

// 配置溯源批次环节 
export const addTraceProductBatchLink = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/addTraceProductBatchLink',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询溯源批次环节（编辑使用）
export const getTraceProductBatchLink = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/template/v1/getTraceProductBatchLink?batchId=' + data,
  })
}

// 编辑溯源批次环节
export const updateTraceProductBatchLink = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/updateTraceProductBatchLink',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取小程序模板商品预览信息（编辑使用）
export const getTraceAppProductInfo = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/getTraceAppProductInfo?batchId=' + data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 配置小程序模板商品预览信息模板
export const addTraceAppProductInfoTemplate = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/addTraceAppProductInfoTemplate',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除未上链溯源商品批次
export const deleteTracePruductBatch = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/template/v1/deleteTracePruductBatch?batchId=' + data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询商品溯源环节（上链使用）
export const getProductTraceLink = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/chain/v1/getProductTraceLink?batchId=' + data,
  })
}

// 溯源信息上链
export const traceLinkUploadToChain = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/chain/v1/traceLinkUploadToChain',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 生成溯源码
export const generateSourceCode = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/chain/v1/generateSourceCode?batchId=' + data,
  })
}

// 导出溯源码报表
export const exportSourceCodeToExcel = (data) => {
  return axios({
    method: 'GET',
    url: apis + '/trace/chain/v1/exportSourceCodeToExcel?batchId=' + data
  })
}

// 查询所有溯源服务（卖家后台使用）
export const getAllTraceService = () => {
  return axios({
    method: 'GET',
    url: apis + '/trace/consume/v1/getAllTraceService'
  })
}

// 溯源码扫描统计（商家）
export const getTraceCodeStati = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/app/v1/getTraceCodeScanStatisticsByPage',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 购买溯源服务
export const buyTraceService = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/consume/v1/buyTraceService',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取CAT对CNY价格
export const getCatCnyRate = () => {
  return axios({
    method: 'GET',
    url: apis + '/trace/consume/v1/getCatCnyRate'
  })
}

// 查询卖家溯源服务
export const getBuyTraceService = () => {
  return axios({
    method: 'GET',
    url: apis + '/trace/consume/v1/getBuyTraceService'
  })
}

// 取消溯源服务订单
export const cancelTraceServiceOrder = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/consume/v1/cancelTraceServiceOrder',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 获取店铺商品推荐列表
export const getStoreRecommendList = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreGoodsRecommend/v2/getStoreRecommendList',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 店铺商品推荐(新增/删除)
export const oPStoreRecommend = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreGoodsRecommend/v2/oPStoreRecommend',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询店铺banner
export const getStoreBanner = (id) => {
  return axios({
    method: 'GET',
    url: apis + '/store/xlStoreBanner/list?storeId=' + id
  })
}

// 查询店铺banner详情
export const getBannerDetail = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreBanner/getBannerDetail',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 删除店铺banner
export const deleteStoreBanner = (id) => {
  return axios({
    method: 'GET',
    url: apis + '/store/xlStoreBanner/delete?id=' + id
  })
}

// 添加店铺banner
export const addStoreBanner = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/store/xlStoreBanner/add',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 刷新溯源服务订单
export const raceServiceOrder = () => {
  return axios({
    method: 'POST',
    url: apis + '/trace/consume/v1/refreshNoPayTraceServiceOrder',
    // data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 查询卖家溯源服务明细（订阅）
export const getBuyTraceServ = () => {
  return axios({
    method: 'GET',
    url: apis + '/trace/consume/v1/getBuyTraceServiceDetail'
  })
}

// 订阅溯源服务
export const upgradeTraceService = (data) => {
  return axios({
    method: 'POST',
    url: apis + '/trace/consume/v1/upgradeTraceService',
    data: JSON.stringify(data),
    headers: {
      'Content-Type': 'application/json'
    }
  })
}
